#include "glWidget.h"

GLWidget::GLWidget(QWidget* parent, unsigned int fps)
  : QGLWidget(parent),
    mFramesPainted(0),
    mTriangleRotation(0),
    mSquareRotation(0)
{
  connect(&mTimer, SIGNAL(timeout()), this, SLOT(updateGL()));
  mTimer.start(1000/fps);
  mTime.start();
}

GLWidget::~GLWidget()
{
  mTimer.stop();
}

void GLWidget::initializeGL()
{
  glShadeModel(GL_SMOOTH); //smooth shading
  glClearColor(0, 0, 0, 0); //black background

  //depth buffer setup
  glClearDepth(1);
  glEnable(GL_DEPTH_TEST);
  glDepthFunc(GL_LEQUAL);

  glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); //best perspective correction
}

void GLWidget::resizeGL(int w, int h)
{
  glViewport(0, 0, w, h); //reset viewport

  //setup perspective view
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(45, w*1./h, 0.1, 100.);

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

void GLWidget::paintGL()
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //clear screen and depth buffer

  glLoadIdentity(); //reset view
  glTranslatef(-1.5, 0, -6); //offset
  glRotatef(mTriangleRotation, 0, 1, 0); //rotate about y axis
  glBegin(GL_TRIANGLES);
    //front face
    glColor3f(1, 0, 0);
    glVertex3f(0, 1, 0);
    glColor3f(0, 1, 0);
    glVertex3f(-1, -1, 1);
    glColor3f(0, 0, 1);
    glVertex3f(1, -1, 1);

    //right face
    glColor3f(1, 0, 0);
    glVertex3f(0, 1, 0);
    glColor3f(0, 0, 1);
    glVertex3f(1, -1, 1);
    glColor3f(0, 1, 0);
    glVertex3f(1, -1, -1);

    //back face
    glColor3f(1, 0, 0);
    glVertex3f(0, 1, 0);
    glColor3f(0, 1, 0);
    glVertex3f(1, -1, -1);
    glColor3f(0, 0, 1);
    glVertex3f(-1, -1, -1);

    //left face
    glColor3f(1, 0, 0);
    glVertex3f(0, 1, 0);
    glColor3f(0, 0, 1);
    glVertex3f(-1, -1, -1);
    glColor3f(0, 1, 0);
    glVertex3f(-1, -1, 1);
  glEnd();

  glLoadIdentity(); //reset view
  glTranslatef(1.5, 0, -7);
  glRotatef(mSquareRotation, 1, 1, 1);
  glBegin(GL_QUADS);
    //top
    glColor3f(0, 1, 0);
    glVertex3f(1, 1, -1);
    glVertex3f(-1, 1, -1);
    glVertex3f(-1, 1, 1);
    glVertex3f(1, 1, 1);

    //bottom
    glColor3f(1, 0.5, 0);
    glVertex3f(1, -1, 1);
    glVertex3f(-1, -1, 1);
    glVertex3f(-1, -1, -1);
    glVertex3f(1, -1, -1);

    //front
    glColor3f(1, 0, 0);
    glVertex3f(1, 1, 1);
    glVertex3f(-1, 1, 1);
    glVertex3f(-1, -1, 1);
    glVertex3f(1, -1, 1);

    //back
    glColor3f(1, 1, 0);
    glVertex3f(1, -1, -1);
    glVertex3f(-1, -1, -1);
    glVertex3f(-1, 1, -1);
    glVertex3f(1, 1, -1);

    //left
    glColor3f(0, 0, 1);
    glVertex3f(-1, 1, 1);
    glVertex3f(-1, 1, -1);
    glVertex3f(-1, -1, -1);
    glVertex3f(-1, -1, 1);

    //right
    glColor3f(1, 0, 1);
    glVertex3f(1, 1, -1);
    glVertex3f(1, 1, 1);
    glVertex3f(1, -1, 1);
    glVertex3f(1, -1, -1);
  glEnd();

  mTriangleRotation += 1;
  mSquareRotation -= 0.5;

  //calculate fps
  ++mFramesPainted;
  if (mTime.elapsed() > 1000)
  {
    emit fps(mFramesPainted * 1000 / mTime.restart());
    mFramesPainted = 0;
  }
}
